
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
        <link rel="prev" href="../04_Bootstrap/index.html">
      
      
        <link rel="next" href="../Java/05_JSP%E7%9A%84%E9%9A%90%E5%90%AB%E5%AF%B9%E8%B1%A1%E5%AE%9E%E9%AA%8C/index.html">
      
      
      <link rel="icon" href="../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.6.1, mkdocs-material-9.6.5">
    
    
      
        <title>06 Vue.js - 基于WEB的应用程序开发-实验指导书</title>
      
    
    
      <link rel="stylesheet" href="../assets/stylesheets/main.8608ea7d.min.css">
      
        
        <link rel="stylesheet" href="../assets/stylesheets/palette.06af60db.min.css">
      
      


    
    
      
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
      <link rel="stylesheet" href="../assets/stylesheets/extra.css">
    
    <script>__md_scope=new URL("..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="purple" data-md-color-accent="indigo">
  
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#vuejs" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

  

<header class="md-header md-header--shadow" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../index.html" title="基于WEB的应用程序开发-实验指导书" class="md-header__button md-logo" aria-label="基于WEB的应用程序开发-实验指导书" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            基于WEB的应用程序开发-实验指导书
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              06 Vue.js
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
  
    
    
    
    <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="purple" data-md-color-accent="indigo"  aria-label="夜间模式"  type="radio" name="__palette" id="__palette_0">
    
      <label class="md-header__button md-icon" title="夜间模式" for="__palette_1" hidden>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5s-1.65.15-2.39.42zM3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29zm.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14zM20.65 7l-1.77 3.79a7.02 7.02 0 0 0-2.38-4.15zm-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29zM12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44z"/></svg>
      </label>
    
  
    
    
    
    <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="teal" data-md-color-accent="indigo"  aria-label="日间模式"  type="radio" name="__palette" id="__palette_1">
    
      <label class="md-header__button md-icon" title="日间模式" for="__palette_0" hidden>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3zm3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95zm-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31"/></svg>
      </label>
    
  
</form>
      
    
    
      <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script>
    
    
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    



<nav class="md-nav md-nav--primary" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../index.html" title="基于WEB的应用程序开发-实验指导书" class="md-nav__button md-logo" aria-label="基于WEB的应用程序开发-实验指导书" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg>

    </a>
    基于WEB的应用程序开发-实验指导书
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    首页
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    
    
    
      
        
        
      
    
    
    <li class="md-nav__item md-nav__item--section md-nav__item--nested">
      
        
        
          
        
        <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2" >
        
          
          <label class="md-nav__link" for="__nav_2" id="__nav_2_label" tabindex="">
            
  
  <span class="md-ellipsis">
    1. 实验环境搭建
    
  </span>
  

            <span class="md-nav__icon md-icon"></span>
          </label>
        
        <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
          <label class="md-nav__title" for="__nav_2">
            <span class="md-nav__icon md-icon"></span>
            1. 实验环境搭建
          </label>
          <ul class="md-nav__list" data-md-scrollfix>
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../00_%E7%8E%AF%E5%A2%83%E5%BB%BA%E7%AB%8B/Java/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    1.1 前端环境搭建
    
  </span>
  

      </a>
    </li>
  

              
            
              
                
  
  
  
  
    <li class="md-nav__item">
      <a href="../Java/05_%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%BC%80%E5%8F%91%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    1.2 后端境搭建
    
  </span>
  

      </a>
    </li>
  

              
            
          </ul>
        </nav>
      
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../01_Html%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    2 Html基础实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../02_CSS%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    3 CSS基础实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../03_Javascript%E5%9F%BA%E7%A1%80/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    04 Javascript基础
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../03_Javascript%E9%AB%98%E7%BA%A7%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    05 Javascript高级实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../04_jQuery/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    06 jQuery
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../04_Bootstrap/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    06 Bootstrap
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
    
  
  
  
    <li class="md-nav__item md-nav__item--active">
      
      <input class="md-nav__toggle md-toggle" type="checkbox" id="__toc">
      
      
        
      
      
        <label class="md-nav__link md-nav__link--active" for="__toc">
          
  
  <span class="md-ellipsis">
    06 Vue.js
    
  </span>
  

          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <a href="index.html" class="md-nav__link md-nav__link--active">
        
  
  <span class="md-ellipsis">
    06 Vue.js
    
  </span>
  

      </a>
      
        

  

<nav class="md-nav md-nav--secondary" aria-label=" ">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
       
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    <span class="md-ellipsis">
      1 实验类型
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    <span class="md-ellipsis">
      2 实验目的
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    <span class="md-ellipsis">
      3 实验要求
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#4" class="md-nav__link">
    <span class="md-ellipsis">
      4 实验环境
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#5" class="md-nav__link">
    <span class="md-ellipsis">
      5 实验步骤
    </span>
  </a>
  
    <nav class="md-nav" aria-label="5 实验步骤">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#hello-vuejs" class="md-nav__link">
    <span class="md-ellipsis">
      Hello Vue.js
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#hello-vuejsnpm" class="md-nav__link">
    <span class="md-ellipsis">
      Hello Vue.js(NPM方式)
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    <span class="md-ellipsis">
      条件语句
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    <span class="md-ellipsis">
      循环语句
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    <span class="md-ellipsis">
      监听属性
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    <span class="md-ellipsis">
      事件处理
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    <span class="md-ellipsis">
      组件
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    <span class="md-ellipsis">
      综合
    </span>
  </a>
  
    <nav class="md-nav" aria-label="综合">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    <span class="md-ellipsis">
      表单校验
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    <span class="md-ellipsis">
      拓展练习
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
      
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../Java/05_JSP%E7%9A%84%E9%9A%90%E5%90%AB%E5%AF%B9%E8%B1%A1%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    05 JSP的隐含对象实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../Java/06_Servlet%E5%8F%8AFilter%E5%9F%BA%E7%A1%80%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    06 Servlet及Filter基础实验
    
  </span>
  

      </a>
    </li>
  

    
      
      
  
  
  
  
    <li class="md-nav__item">
      <a href="../Java/07_SpringBoot%E7%BB%BC%E5%90%88%E5%AE%9E%E9%AA%8C/index.html" class="md-nav__link">
        
  
  <span class="md-ellipsis">
    07 Spring Boot综合实验
    
  </span>
  

      </a>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  

<nav class="md-nav md-nav--secondary" aria-label=" ">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
       
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#1" class="md-nav__link">
    <span class="md-ellipsis">
      1 实验类型
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    <span class="md-ellipsis">
      2 实验目的
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#3" class="md-nav__link">
    <span class="md-ellipsis">
      3 实验要求
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#4" class="md-nav__link">
    <span class="md-ellipsis">
      4 实验环境
    </span>
  </a>
  
</li>
      
        <li class="md-nav__item">
  <a href="#5" class="md-nav__link">
    <span class="md-ellipsis">
      5 实验步骤
    </span>
  </a>
  
    <nav class="md-nav" aria-label="5 实验步骤">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#hello-vuejs" class="md-nav__link">
    <span class="md-ellipsis">
      Hello Vue.js
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#hello-vuejsnpm" class="md-nav__link">
    <span class="md-ellipsis">
      Hello Vue.js(NPM方式)
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_1" class="md-nav__link">
    <span class="md-ellipsis">
      条件语句
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_2" class="md-nav__link">
    <span class="md-ellipsis">
      循环语句
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_3" class="md-nav__link">
    <span class="md-ellipsis">
      监听属性
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_4" class="md-nav__link">
    <span class="md-ellipsis">
      事件处理
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_5" class="md-nav__link">
    <span class="md-ellipsis">
      组件
    </span>
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_6" class="md-nav__link">
    <span class="md-ellipsis">
      综合
    </span>
  </a>
  
    <nav class="md-nav" aria-label="综合">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#_7" class="md-nav__link">
    <span class="md-ellipsis">
      表单校验
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
        
          <li class="md-nav__item">
  <a href="#_8" class="md-nav__link">
    <span class="md-ellipsis">
      拓展练习
    </span>
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  



<h1 id="vuejs">Vue.js基础实验</h1>
<h2 id="1">1 实验类型</h2>
<p>验证型，2学时，必选实验</p>
<h2 id="2">2 实验目的</h2>
<p>熟悉Vue.js基本语法；了解Vue.js组件编程模式；</p>
<h2 id="3">3 实验要求</h2>
<p>验证参考代码；修改参考代码，实现自己的设计；将关键结果截图及源代码整理成实验报告</p>
<h2 id="4">4 实验环境</h2>
<p>Windows7、<code>Microsoft Edge/Chrome/Firefox</code>等浏览器、Visual Studio Code、Vue.js 3+、Node.js、NPM</p>
<blockquote>
<p><strong>vue.js下载地址</strong>：<a href="https://unpkg.com/vue@3/dist/vue.global.js">https://unpkg.com/vue@3/dist/vue.global.js</a></p>
</blockquote>
<h2 id="5">5 实验步骤</h2>
<blockquote>
<p>工作目录：<code>C:/{学号}/exp_5/vuejs</code>， 后续文件均存放在工作目录</p>
</blockquote>
<h3 id="hello-vuejs">Hello Vue.js</h3>
<ol>
<li>
<p>将<strong>Vue.js</strong>放入该工作目录</p>
</li>
<li>
<p>将<code>jquery-3.7.1.min.js</code>放到工作目录</p>
<blockquote>
<p>注意：若参考代码中引用的文件路径与实际不符时，请按实际情况调整</p>
</blockquote>
</li>
<li>
<p>创建文件<strong>Hello.html</strong>并输入<code>Hello Vue.js</code>代码，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">vue_Hello.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-0-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-0-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-0-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-0-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-0-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-0-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-0-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-0-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-0-10">10</a></span>
<span class="normal"><a href="#__codelineno-0-11">11</a></span>
<span class="normal"><a href="#__codelineno-0-12">12</a></span>
<span class="normal"><a href="#__codelineno-0-13">13</a></span>
<span class="normal"><a href="#__codelineno-0-14">14</a></span>
<span class="normal"><a href="#__codelineno-0-15">15</a></span>
<span class="normal"><a href="#__codelineno-0-16">16</a></span>
<span class="normal"><a href="#__codelineno-0-17">17</a></span>
<span class="normal"><a href="#__codelineno-0-18">18</a></span>
<span class="normal"><a href="#__codelineno-0-19">19</a></span>
<span class="normal"><a href="#__codelineno-0-20">20</a></span>
<span class="normal"><a href="#__codelineno-0-21">21</a></span>
<span class="normal"><a href="#__codelineno-0-22">22</a></span>
<span class="normal"><a href="#__codelineno-0-23">23</a></span>
<span class="normal"><a href="#__codelineno-0-24">24</a></span>
<span class="normal"><a href="#__codelineno-0-25">25</a></span>
<span class="normal"><a href="#__codelineno-0-26">26</a></span>
<span class="normal"><a href="#__codelineno-0-27">27</a></span>
<span class="normal"><a href="#__codelineno-0-28">28</a></span>
<span class="normal"><a href="#__codelineno-0-29">29</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-0-2" name="__codelineno-0-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-0-3" name="__codelineno-0-3"></a>
<a id="__codelineno-0-4" name="__codelineno-0-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-0-5" name="__codelineno-0-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-0-6" name="__codelineno-0-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>Hello Vue.js<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-0-7" name="__codelineno-0-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-0-8" name="__codelineno-0-8"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-0-9" name="__codelineno-0-9"></a>
<a id="__codelineno-0-10" name="__codelineno-0-10"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-0-11" name="__codelineno-0-11"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;hello-vue&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;demo&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-0-12" name="__codelineno-0-12"></a>    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>{{ message }}<span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-0-13" name="__codelineno-0-13"></a>    <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="na">v-model</span><span class="o">=</span><span class="s">&quot;message&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-0-14" name="__codelineno-0-14"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-0-15" name="__codelineno-0-15"></a>
<a id="__codelineno-0-16" name="__codelineno-0-16"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-0-17" name="__codelineno-0-17"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span>
<a id="__codelineno-0-18" name="__codelineno-0-18"></a><span class="w">      </span><span class="nx">data</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-19" name="__codelineno-0-19"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-0-20" name="__codelineno-0-20"></a><span class="w">          </span><span class="nx">message</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Hello Vue.js!&#39;</span>
<a id="__codelineno-0-21" name="__codelineno-0-21"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-0-22" name="__codelineno-0-22"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-0-23" name="__codelineno-0-23"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-0-24" name="__codelineno-0-24"></a>
<a id="__codelineno-0-25" name="__codelineno-0-25"></a><span class="w">    </span><span class="nx">vm</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#hello-vue&#39;</span><span class="p">)</span>
<a id="__codelineno-0-26" name="__codelineno-0-26"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-0-27" name="__codelineno-0-27"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-0-28" name="__codelineno-0-28"></a>
<a id="__codelineno-0-29" name="__codelineno-0-29"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>修改文本框内的字符串观察界面变化</p>
</li>
</ol>
<h3 id="hello-vuejsnpm">Hello Vue.js(NPM方式)</h3>
<ol>
<li>
<p>安装<strong>Node.js</strong></p>
</li>
<li>
<p>安装<strong>vue-cli</strong></p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a>npm<span class="w"> </span>install<span class="w"> </span>-g<span class="w"> </span>@vue/cli
</code></pre></div>
</li>
<li>
<p>查看<strong>Vue</strong>版本</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1" href="#__codelineno-2-1"></a>vue<span class="w"> </span>--version
</code></pre></div>
</li>
<li>
<p>安装<strong>@vue/cli-init</strong></p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1" href="#__codelineno-3-1"></a>npm<span class="w"> </span>i<span class="w"> </span>-g<span class="w"> </span>@vue/cli-init
</code></pre></div>
</li>
<li>
<p>创建<strong>Vue</strong>应用</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1" href="#__codelineno-4-1"></a>vue<span class="w"> </span>init<span class="w"> </span>webpack<span class="w"> </span>vue-cli-test<span class="w">    </span>
<a id="__codelineno-4-2" name="__codelineno-4-2" href="#__codelineno-4-2"></a>?<span class="w"> </span>Project<span class="w"> </span>name<span class="w"> </span>vue-cli-test
<a id="__codelineno-4-3" name="__codelineno-4-3" href="#__codelineno-4-3"></a>?<span class="w"> </span>Project<span class="w"> </span>description<span class="w"> </span>Hello<span class="w"> </span>Vue-Cli
<a id="__codelineno-4-4" name="__codelineno-4-4" href="#__codelineno-4-4"></a>?<span class="w"> </span>Author<span class="w"> </span><span class="m">86263008</span><span class="w"> </span>&lt;<span class="m">86263008</span>&gt;
<a id="__codelineno-4-5" name="__codelineno-4-5" href="#__codelineno-4-5"></a>?<span class="w"> </span>Vue<span class="w"> </span>build<span class="w"> </span>standalone<span class="w">      </span>
<a id="__codelineno-4-6" name="__codelineno-4-6" href="#__codelineno-4-6"></a>?<span class="w"> </span>Install<span class="w"> </span>vue-router?<span class="w"> </span>Yes
<a id="__codelineno-4-7" name="__codelineno-4-7" href="#__codelineno-4-7"></a>?<span class="w"> </span>Use<span class="w"> </span>ESLint<span class="w"> </span>to<span class="w"> </span>lint<span class="w"> </span>your<span class="w"> </span>code?<span class="w"> </span>No
<a id="__codelineno-4-8" name="__codelineno-4-8" href="#__codelineno-4-8"></a>?<span class="w"> </span>Set<span class="w"> </span>up<span class="w"> </span>unit<span class="w"> </span>tests<span class="w"> </span>No
<a id="__codelineno-4-9" name="__codelineno-4-9" href="#__codelineno-4-9"></a>?<span class="w"> </span>Setup<span class="w"> </span>e2e<span class="w"> </span>tests<span class="w"> </span>with<span class="w"> </span>Nightwatch?<span class="w"> </span>No
<a id="__codelineno-4-10" name="__codelineno-4-10" href="#__codelineno-4-10"></a>?<span class="w"> </span>Should<span class="w"> </span>we<span class="w"> </span>run<span class="w"> </span><span class="sb">`</span>npm<span class="w"> </span>install<span class="sb">`</span><span class="w"> </span><span class="k">for</span><span class="w"> </span>you<span class="w"> </span>after<span class="w"> </span>the<span class="w"> </span>project<span class="w"> </span>has<span class="w"> </span>been<span class="w"> </span>created?<span class="w"> </span><span class="o">(</span>recommended<span class="o">)</span><span class="w"> </span>npm
</code></pre></div>
</li>
<li>
<p>进入<strong>vue-cli-test</strong>目录，启动应用：</p>
<div class="highlight"><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1" href="#__codelineno-5-1"></a>npm<span class="w"> </span>run<span class="w"> </span>dev
</code></pre></div>
</li>
<li>
<p>在浏览器中访问<code>http://localhost:8080</code></p>
</li>
</ol>
<h3 id="_1">条件语句</h3>
<ol>
<li>
<p>创建<strong>Conditional.html</strong>，生成随机数并根据随机数值的不同显示不同的字符串，参考代码如下:</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">vue_Conditional.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-6-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-6-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-6-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-6-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-6-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-6-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-6-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-6-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-6-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-6-10">10</a></span>
<span class="normal"><a href="#__codelineno-6-11">11</a></span>
<span class="normal"><a href="#__codelineno-6-12">12</a></span>
<span class="normal"><a href="#__codelineno-6-13">13</a></span>
<span class="normal"><a href="#__codelineno-6-14">14</a></span>
<span class="normal"><a href="#__codelineno-6-15">15</a></span>
<span class="normal"><a href="#__codelineno-6-16">16</a></span>
<span class="normal"><a href="#__codelineno-6-17">17</a></span>
<span class="normal"><a href="#__codelineno-6-18">18</a></span>
<span class="normal"><a href="#__codelineno-6-19">19</a></span>
<span class="normal"><a href="#__codelineno-6-20">20</a></span>
<span class="normal"><a href="#__codelineno-6-21">21</a></span>
<span class="normal"><a href="#__codelineno-6-22">22</a></span>
<span class="normal"><a href="#__codelineno-6-23">23</a></span>
<span class="normal"><a href="#__codelineno-6-24">24</a></span>
<span class="normal"><a href="#__codelineno-6-25">25</a></span>
<span class="normal"><a href="#__codelineno-6-26">26</a></span>
<span class="normal"><a href="#__codelineno-6-27">27</a></span>
<span class="normal"><a href="#__codelineno-6-28">28</a></span>
<span class="normal"><a href="#__codelineno-6-29">29</a></span>
<span class="normal"><a href="#__codelineno-6-30">30</a></span>
<span class="normal"><a href="#__codelineno-6-31">31</a></span>
<span class="normal"><a href="#__codelineno-6-32">32</a></span>
<span class="normal"><a href="#__codelineno-6-33">33</a></span>
<span class="normal"><a href="#__codelineno-6-34">34</a></span>
<span class="normal"><a href="#__codelineno-6-35">35</a></span>
<span class="normal"><a href="#__codelineno-6-36">36</a></span>
<span class="normal"><a href="#__codelineno-6-37">37</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-6-2" name="__codelineno-6-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-6-3" name="__codelineno-6-3"></a>
<a id="__codelineno-6-4" name="__codelineno-6-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-6-5" name="__codelineno-6-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-6-6" name="__codelineno-6-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>条件语句<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-6-7" name="__codelineno-6-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-6-8" name="__codelineno-6-8"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-6-9" name="__codelineno-6-9"></a>
<a id="__codelineno-6-10" name="__codelineno-6-10"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-6-11" name="__codelineno-6-11"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;app&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-6-12" name="__codelineno-6-12"></a>    <span class="p">&lt;</span><span class="nt">button</span> <span class="err">@</span><span class="na">click</span><span class="o">=</span><span class="s">&quot;yao&quot;</span><span class="p">&gt;</span>摇<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-6-13" name="__codelineno-6-13"></a>    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>{{rnd}}<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-6-14" name="__codelineno-6-14"></a>    <span class="p">&lt;</span><span class="nt">p</span> <span class="na">v-if</span><span class="o">=</span><span class="s">&quot;rnd &gt;= 0.5&quot;</span><span class="p">&gt;</span>这把是大<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-6-15" name="__codelineno-6-15"></a>    <span class="p">&lt;</span><span class="nt">p</span> <span class="na">v-else</span><span class="p">&gt;</span>这把是小<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-6-16" name="__codelineno-6-16"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-6-17" name="__codelineno-6-17"></a>
<a id="__codelineno-6-18" name="__codelineno-6-18"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-6-19" name="__codelineno-6-19"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span>
<a id="__codelineno-6-20" name="__codelineno-6-20"></a><span class="w">      </span><span class="nx">data</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-21" name="__codelineno-6-21"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-22" name="__codelineno-6-22"></a><span class="w">          </span><span class="nx">rnd</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span>
<a id="__codelineno-6-23" name="__codelineno-6-23"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-6-24" name="__codelineno-6-24"></a><span class="w">      </span><span class="p">},</span>
<a id="__codelineno-6-25" name="__codelineno-6-25"></a><span class="w">      </span><span class="nx">methods</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-26" name="__codelineno-6-26"></a><span class="w">        </span><span class="nx">yao</span><span class="p">(</span><span class="nx">event</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-6-27" name="__codelineno-6-27"></a><span class="w">          </span><span class="k">this</span><span class="p">.</span><span class="nx">rnd</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Math</span><span class="p">.</span><span class="nx">random</span><span class="p">();</span>
<a id="__codelineno-6-28" name="__codelineno-6-28"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-6-29" name="__codelineno-6-29"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-6-30" name="__codelineno-6-30"></a><span class="w">    </span><span class="p">}</span>
<a id="__codelineno-6-31" name="__codelineno-6-31"></a><span class="w">    </span><span class="p">);</span>
<a id="__codelineno-6-32" name="__codelineno-6-32"></a>
<a id="__codelineno-6-33" name="__codelineno-6-33"></a><span class="w">    </span><span class="nx">vm</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">);</span>
<a id="__codelineno-6-34" name="__codelineno-6-34"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-6-35" name="__codelineno-6-35"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-6-36" name="__codelineno-6-36"></a>
<a id="__codelineno-6-37" name="__codelineno-6-37"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
</ol>
<h3 id="_2">循环语句</h3>
<ol>
<li>
<p>创建<strong>Loop.html</strong>文件，使用循环命令创建有序列表，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">vue_Loop.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-7-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-7-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-7-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-7-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-7-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-7-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-7-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-7-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-7-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-7-10">10</a></span>
<span class="normal"><a href="#__codelineno-7-11">11</a></span>
<span class="normal"><a href="#__codelineno-7-12">12</a></span>
<span class="normal"><a href="#__codelineno-7-13">13</a></span>
<span class="normal"><a href="#__codelineno-7-14">14</a></span>
<span class="normal"><a href="#__codelineno-7-15">15</a></span>
<span class="normal"><a href="#__codelineno-7-16">16</a></span>
<span class="normal"><a href="#__codelineno-7-17">17</a></span>
<span class="normal"><a href="#__codelineno-7-18">18</a></span>
<span class="normal"><a href="#__codelineno-7-19">19</a></span>
<span class="normal"><a href="#__codelineno-7-20">20</a></span>
<span class="normal"><a href="#__codelineno-7-21">21</a></span>
<span class="normal"><a href="#__codelineno-7-22">22</a></span>
<span class="normal"><a href="#__codelineno-7-23">23</a></span>
<span class="normal"><a href="#__codelineno-7-24">24</a></span>
<span class="normal"><a href="#__codelineno-7-25">25</a></span>
<span class="normal"><a href="#__codelineno-7-26">26</a></span>
<span class="normal"><a href="#__codelineno-7-27">27</a></span>
<span class="normal"><a href="#__codelineno-7-28">28</a></span>
<span class="normal"><a href="#__codelineno-7-29">29</a></span>
<span class="normal"><a href="#__codelineno-7-30">30</a></span>
<span class="normal"><a href="#__codelineno-7-31">31</a></span>
<span class="normal"><a href="#__codelineno-7-32">32</a></span>
<span class="normal"><a href="#__codelineno-7-33">33</a></span>
<span class="normal"><a href="#__codelineno-7-34">34</a></span>
<span class="normal"><a href="#__codelineno-7-35">35</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-7-2" name="__codelineno-7-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-7-3" name="__codelineno-7-3"></a>
<a id="__codelineno-7-4" name="__codelineno-7-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-7-5" name="__codelineno-7-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-7-6" name="__codelineno-7-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>循环语句<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-7-7" name="__codelineno-7-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-7-8" name="__codelineno-7-8"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-7-9" name="__codelineno-7-9"></a>
<a id="__codelineno-7-10" name="__codelineno-7-10"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-7-11" name="__codelineno-7-11"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;app&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-7-12" name="__codelineno-7-12"></a>    <span class="p">&lt;</span><span class="nt">ol</span><span class="p">&gt;</span>
<a id="__codelineno-7-13" name="__codelineno-7-13"></a>      <span class="p">&lt;</span><span class="nt">li</span> <span class="na">v-for</span><span class="o">=</span><span class="s">&quot;todo in todos&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-7-14" name="__codelineno-7-14"></a>        {{ todo.text }}
<a id="__codelineno-7-15" name="__codelineno-7-15"></a>      <span class="p">&lt;/</span><span class="nt">li</span><span class="p">&gt;</span>
<a id="__codelineno-7-16" name="__codelineno-7-16"></a>    <span class="p">&lt;/</span><span class="nt">ol</span><span class="p">&gt;</span>
<a id="__codelineno-7-17" name="__codelineno-7-17"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-7-18" name="__codelineno-7-18"></a>
<a id="__codelineno-7-19" name="__codelineno-7-19"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-7-20" name="__codelineno-7-20"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span>
<a id="__codelineno-7-21" name="__codelineno-7-21"></a><span class="w">      </span><span class="nx">data</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-22" name="__codelineno-7-22"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-7-23" name="__codelineno-7-23"></a><span class="w">          </span><span class="nx">todos</span><span class="o">:</span><span class="w"> </span><span class="p">[</span>
<a id="__codelineno-7-24" name="__codelineno-7-24"></a><span class="w">            </span><span class="p">{</span><span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Learn JavaScript&#39;</span><span class="w"> </span><span class="p">},</span>
<a id="__codelineno-7-25" name="__codelineno-7-25"></a><span class="w">            </span><span class="p">{</span><span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Learn Vue&#39;</span><span class="w"> </span><span class="p">},</span>
<a id="__codelineno-7-26" name="__codelineno-7-26"></a><span class="w">            </span><span class="p">{</span><span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Build something awesome&#39;</span><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-7-27" name="__codelineno-7-27"></a><span class="w">          </span><span class="p">]</span>
<a id="__codelineno-7-28" name="__codelineno-7-28"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-7-29" name="__codelineno-7-29"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-7-30" name="__codelineno-7-30"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-7-31" name="__codelineno-7-31"></a><span class="w">    </span><span class="nx">vm</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">)</span>
<a id="__codelineno-7-32" name="__codelineno-7-32"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-7-33" name="__codelineno-7-33"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-7-34" name="__codelineno-7-34"></a>
<a id="__codelineno-7-35" name="__codelineno-7-35"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
</ol>
<h3 id="_3">监听属性</h3>
<ol>
<li>
<p>创建<strong>Watcher.html</strong>文件，通过属性监听实现单位换算，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">vue_Watcher.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-8-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-8-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-8-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-8-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-8-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-8-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-8-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-8-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-8-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-8-10">10</a></span>
<span class="normal"><a href="#__codelineno-8-11">11</a></span>
<span class="normal"><a href="#__codelineno-8-12">12</a></span>
<span class="normal"><a href="#__codelineno-8-13">13</a></span>
<span class="normal"><a href="#__codelineno-8-14">14</a></span>
<span class="normal"><a href="#__codelineno-8-15">15</a></span>
<span class="normal"><a href="#__codelineno-8-16">16</a></span>
<span class="normal"><a href="#__codelineno-8-17">17</a></span>
<span class="normal"><a href="#__codelineno-8-18">18</a></span>
<span class="normal"><a href="#__codelineno-8-19">19</a></span>
<span class="normal"><a href="#__codelineno-8-20">20</a></span>
<span class="normal"><a href="#__codelineno-8-21">21</a></span>
<span class="normal"><a href="#__codelineno-8-22">22</a></span>
<span class="normal"><a href="#__codelineno-8-23">23</a></span>
<span class="normal"><a href="#__codelineno-8-24">24</a></span>
<span class="normal"><a href="#__codelineno-8-25">25</a></span>
<span class="normal"><a href="#__codelineno-8-26">26</a></span>
<span class="normal"><a href="#__codelineno-8-27">27</a></span>
<span class="normal"><a href="#__codelineno-8-28">28</a></span>
<span class="normal"><a href="#__codelineno-8-29">29</a></span>
<span class="normal"><a href="#__codelineno-8-30">30</a></span>
<span class="normal"><a href="#__codelineno-8-31">31</a></span>
<span class="normal"><a href="#__codelineno-8-32">32</a></span>
<span class="normal"><a href="#__codelineno-8-33">33</a></span>
<span class="normal"><a href="#__codelineno-8-34">34</a></span>
<span class="normal"><a href="#__codelineno-8-35">35</a></span>
<span class="normal"><a href="#__codelineno-8-36">36</a></span>
<span class="normal"><a href="#__codelineno-8-37">37</a></span>
<span class="normal"><a href="#__codelineno-8-38">38</a></span>
<span class="normal"><a href="#__codelineno-8-39">39</a></span>
<span class="normal"><a href="#__codelineno-8-40">40</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-8-2" name="__codelineno-8-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-8-3" name="__codelineno-8-3"></a>
<a id="__codelineno-8-4" name="__codelineno-8-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-8-5" name="__codelineno-8-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-8-6" name="__codelineno-8-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>监听属性<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-8-7" name="__codelineno-8-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-8-8" name="__codelineno-8-8"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-8-9" name="__codelineno-8-9"></a>
<a id="__codelineno-8-10" name="__codelineno-8-10"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-8-11" name="__codelineno-8-11"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;app&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-8-12" name="__codelineno-8-12"></a>    千米 : <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="na">v-model</span><span class="o">=</span><span class="s">&quot;kilometers&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-8-13" name="__codelineno-8-13"></a>    米 : <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="na">v-model</span><span class="o">=</span><span class="s">&quot;meters&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-8-14" name="__codelineno-8-14"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-8-15" name="__codelineno-8-15"></a>  <span class="p">&lt;</span><span class="nt">p</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;info&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-8-16" name="__codelineno-8-16"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text/javascript&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-8-17" name="__codelineno-8-17"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">app</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span>
<a id="__codelineno-8-18" name="__codelineno-8-18"></a><span class="w">      </span><span class="nx">data</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-19" name="__codelineno-8-19"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-20" name="__codelineno-8-20"></a><span class="w">          </span><span class="nx">kilometers</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<a id="__codelineno-8-21" name="__codelineno-8-21"></a><span class="w">          </span><span class="nx">meters</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span>
<a id="__codelineno-8-22" name="__codelineno-8-22"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-8-23" name="__codelineno-8-23"></a><span class="w">      </span><span class="p">},</span>
<a id="__codelineno-8-24" name="__codelineno-8-24"></a><span class="w">      </span><span class="nx">watch</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-25" name="__codelineno-8-25"></a><span class="w">        </span><span class="nx">kilometers</span><span class="o">:</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-26" name="__codelineno-8-26"></a><span class="w">          </span><span class="k">this</span><span class="p">.</span><span class="nx">kilometers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">val</span><span class="p">;</span>
<a id="__codelineno-8-27" name="__codelineno-8-27"></a><span class="w">          </span><span class="k">this</span><span class="p">.</span><span class="nx">meters</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">this</span><span class="p">.</span><span class="nx">kilometers</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mf">1000</span>
<a id="__codelineno-8-28" name="__codelineno-8-28"></a><span class="w">        </span><span class="p">},</span>
<a id="__codelineno-8-29" name="__codelineno-8-29"></a><span class="w">        </span><span class="nx">meters</span><span class="o">:</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">val</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-8-30" name="__codelineno-8-30"></a><span class="w">          </span><span class="k">this</span><span class="p">.</span><span class="nx">kilometers</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">val</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mf">1000</span><span class="p">;</span>
<a id="__codelineno-8-31" name="__codelineno-8-31"></a><span class="w">          </span><span class="k">this</span><span class="p">.</span><span class="nx">meters</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">val</span><span class="p">;</span>
<a id="__codelineno-8-32" name="__codelineno-8-32"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-8-33" name="__codelineno-8-33"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-8-34" name="__codelineno-8-34"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-8-35" name="__codelineno-8-35"></a>
<a id="__codelineno-8-36" name="__codelineno-8-36"></a><span class="w">    </span><span class="nx">app</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">)</span>
<a id="__codelineno-8-37" name="__codelineno-8-37"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-8-38" name="__codelineno-8-38"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-8-39" name="__codelineno-8-39"></a>
<a id="__codelineno-8-40" name="__codelineno-8-40"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
</ol>
<h3 id="_4">事件处理</h3>
<ol>
<li>
<p>创建<strong>Event.html</strong>，利用事件处理实现两个数据的加法，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">vue_Event.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-9-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-9-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-9-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-9-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-9-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-9-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-9-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-9-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-9-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-9-10">10</a></span>
<span class="normal"><a href="#__codelineno-9-11">11</a></span>
<span class="normal"><a href="#__codelineno-9-12">12</a></span>
<span class="normal"><a href="#__codelineno-9-13">13</a></span>
<span class="normal"><a href="#__codelineno-9-14">14</a></span>
<span class="normal"><a href="#__codelineno-9-15">15</a></span>
<span class="normal"><a href="#__codelineno-9-16">16</a></span>
<span class="normal"><a href="#__codelineno-9-17">17</a></span>
<span class="normal"><a href="#__codelineno-9-18">18</a></span>
<span class="normal"><a href="#__codelineno-9-19">19</a></span>
<span class="normal"><a href="#__codelineno-9-20">20</a></span>
<span class="normal"><a href="#__codelineno-9-21">21</a></span>
<span class="normal"><a href="#__codelineno-9-22">22</a></span>
<span class="normal"><a href="#__codelineno-9-23">23</a></span>
<span class="normal"><a href="#__codelineno-9-24">24</a></span>
<span class="normal"><a href="#__codelineno-9-25">25</a></span>
<span class="normal"><a href="#__codelineno-9-26">26</a></span>
<span class="normal"><a href="#__codelineno-9-27">27</a></span>
<span class="normal"><a href="#__codelineno-9-28">28</a></span>
<span class="normal"><a href="#__codelineno-9-29">29</a></span>
<span class="normal"><a href="#__codelineno-9-30">30</a></span>
<span class="normal"><a href="#__codelineno-9-31">31</a></span>
<span class="normal"><a href="#__codelineno-9-32">32</a></span>
<span class="normal"><a href="#__codelineno-9-33">33</a></span>
<span class="normal"><a href="#__codelineno-9-34">34</a></span>
<span class="normal"><a href="#__codelineno-9-35">35</a></span>
<span class="normal"><a href="#__codelineno-9-36">36</a></span>
<span class="normal"><a href="#__codelineno-9-37">37</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-9-2" name="__codelineno-9-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-9-3" name="__codelineno-9-3"></a>
<a id="__codelineno-9-4" name="__codelineno-9-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-9-5" name="__codelineno-9-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-9-6" name="__codelineno-9-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>事件处理<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-9-7" name="__codelineno-9-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-9-8" name="__codelineno-9-8"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-9-9" name="__codelineno-9-9"></a>
<a id="__codelineno-9-10" name="__codelineno-9-10"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-9-11" name="__codelineno-9-11"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;app&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-9-12" name="__codelineno-9-12"></a>    数1：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="na">v-model</span><span class="o">=</span><span class="s">&quot;num1&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-9-13" name="__codelineno-9-13"></a>    数2：<span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="na">v-model</span><span class="o">=</span><span class="s">&quot;num2&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-9-14" name="__codelineno-9-14"></a>    <span class="p">&lt;</span><span class="nt">button</span> <span class="err">@</span><span class="na">click</span><span class="o">=</span><span class="s">&quot;add&quot;</span><span class="p">&gt;</span>加<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
<a id="__codelineno-9-15" name="__codelineno-9-15"></a>    <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>{{result}}<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
<a id="__codelineno-9-16" name="__codelineno-9-16"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-9-17" name="__codelineno-9-17"></a>
<a id="__codelineno-9-18" name="__codelineno-9-18"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-9-19" name="__codelineno-9-19"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span>
<a id="__codelineno-9-20" name="__codelineno-9-20"></a><span class="w">      </span><span class="nx">data</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-21" name="__codelineno-9-21"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-22" name="__codelineno-9-22"></a><span class="w">          </span><span class="nx">num1</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<a id="__codelineno-9-23" name="__codelineno-9-23"></a><span class="w">          </span><span class="nx">num2</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span><span class="p">,</span>
<a id="__codelineno-9-24" name="__codelineno-9-24"></a><span class="w">          </span><span class="nx">result</span><span class="o">:</span><span class="w"> </span><span class="mf">0</span>
<a id="__codelineno-9-25" name="__codelineno-9-25"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-9-26" name="__codelineno-9-26"></a><span class="w">      </span><span class="p">},</span>
<a id="__codelineno-9-27" name="__codelineno-9-27"></a><span class="w">      </span><span class="nx">methods</span><span class="o">:</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-28" name="__codelineno-9-28"></a><span class="w">        </span><span class="nx">add</span><span class="p">(</span><span class="nx">event</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-9-29" name="__codelineno-9-29"></a><span class="w">          </span><span class="k">this</span><span class="p">.</span><span class="nx">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">parseFloat</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">num1</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">parseFloat</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">num2</span><span class="p">);</span>
<a id="__codelineno-9-30" name="__codelineno-9-30"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-9-31" name="__codelineno-9-31"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-9-32" name="__codelineno-9-32"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-9-33" name="__codelineno-9-33"></a><span class="w">    </span><span class="nx">vm</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">)</span>
<a id="__codelineno-9-34" name="__codelineno-9-34"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-9-35" name="__codelineno-9-35"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-9-36" name="__codelineno-9-36"></a>
<a id="__codelineno-9-37" name="__codelineno-9-37"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
<li>
<p>在步骤1基础上实现减乘除法</p>
</li>
</ol>
<h3 id="_5">组件</h3>
<ol>
<li>
<p>创建<strong>Component.html</strong>，将<strong>Loop.html</strong>中列表改造为组件，参考代码如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">vue_Component.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-10-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-10-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-10-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-10-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-10-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-10-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-10-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-10-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-10-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-10-10">10</a></span>
<span class="normal"><a href="#__codelineno-10-11">11</a></span>
<span class="normal"><a href="#__codelineno-10-12">12</a></span>
<span class="normal"><a href="#__codelineno-10-13">13</a></span>
<span class="normal"><a href="#__codelineno-10-14">14</a></span>
<span class="normal"><a href="#__codelineno-10-15">15</a></span>
<span class="normal"><a href="#__codelineno-10-16">16</a></span>
<span class="normal"><a href="#__codelineno-10-17">17</a></span>
<span class="normal"><a href="#__codelineno-10-18">18</a></span>
<span class="normal"><a href="#__codelineno-10-19">19</a></span>
<span class="normal"><a href="#__codelineno-10-20">20</a></span>
<span class="normal"><a href="#__codelineno-10-21">21</a></span>
<span class="normal"><a href="#__codelineno-10-22">22</a></span>
<span class="normal"><a href="#__codelineno-10-23">23</a></span>
<span class="normal"><a href="#__codelineno-10-24">24</a></span>
<span class="normal"><a href="#__codelineno-10-25">25</a></span>
<span class="normal"><a href="#__codelineno-10-26">26</a></span>
<span class="normal"><a href="#__codelineno-10-27">27</a></span>
<span class="normal"><a href="#__codelineno-10-28">28</a></span>
<span class="normal"><a href="#__codelineno-10-29">29</a></span>
<span class="normal"><a href="#__codelineno-10-30">30</a></span>
<span class="normal"><a href="#__codelineno-10-31">31</a></span>
<span class="normal"><a href="#__codelineno-10-32">32</a></span>
<span class="normal"><a href="#__codelineno-10-33">33</a></span>
<span class="normal"><a href="#__codelineno-10-34">34</a></span>
<span class="normal"><a href="#__codelineno-10-35">35</a></span>
<span class="normal"><a href="#__codelineno-10-36">36</a></span>
<span class="normal"><a href="#__codelineno-10-37">37</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>
<a id="__codelineno-10-2" name="__codelineno-10-2"></a><span class="p">&lt;</span><span class="nt">html</span><span class="p">&gt;</span>
<a id="__codelineno-10-3" name="__codelineno-10-3"></a>
<a id="__codelineno-10-4" name="__codelineno-10-4"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-10-5" name="__codelineno-10-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;utf-8&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-10-6" name="__codelineno-10-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>组件<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>
<a id="__codelineno-10-7" name="__codelineno-10-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-10-8" name="__codelineno-10-8"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>
<a id="__codelineno-10-9" name="__codelineno-10-9"></a>
<a id="__codelineno-10-10" name="__codelineno-10-10"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-10-11" name="__codelineno-10-11"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;app&quot;</span><span class="p">&gt;</span>
<a id="__codelineno-10-12" name="__codelineno-10-12"></a>    <span class="p">&lt;</span><span class="nt">ol</span><span class="p">&gt;</span>
<a id="__codelineno-10-13" name="__codelineno-10-13"></a>      <span class="p">&lt;</span><span class="nt">todo-item</span> <span class="na">v-for</span><span class="o">=</span><span class="s">&quot;item in todos&quot;</span> <span class="na">v-bind:todo</span><span class="o">=</span><span class="s">&quot;item&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">todo-item</span><span class="p">&gt;</span>
<a id="__codelineno-10-14" name="__codelineno-10-14"></a>    <span class="p">&lt;/</span><span class="nt">ol</span><span class="p">&gt;</span>
<a id="__codelineno-10-15" name="__codelineno-10-15"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
<a id="__codelineno-10-16" name="__codelineno-10-16"></a>
<a id="__codelineno-10-17" name="__codelineno-10-17"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-10-18" name="__codelineno-10-18"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span>
<a id="__codelineno-10-19" name="__codelineno-10-19"></a><span class="w">      </span><span class="nx">data</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-10-20" name="__codelineno-10-20"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-10-21" name="__codelineno-10-21"></a><span class="w">          </span><span class="nx">todos</span><span class="o">:</span><span class="w"> </span><span class="p">[</span>
<a id="__codelineno-10-22" name="__codelineno-10-22"></a><span class="w">            </span><span class="p">{</span><span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Learn JavaScript&#39;</span><span class="w"> </span><span class="p">},</span>
<a id="__codelineno-10-23" name="__codelineno-10-23"></a><span class="w">            </span><span class="p">{</span><span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Learn Vue&#39;</span><span class="w"> </span><span class="p">},</span>
<a id="__codelineno-10-24" name="__codelineno-10-24"></a><span class="w">            </span><span class="p">{</span><span class="w"> </span><span class="nx">text</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Build something awesome&#39;</span><span class="w"> </span><span class="p">}</span>
<a id="__codelineno-10-25" name="__codelineno-10-25"></a><span class="w">          </span><span class="p">]</span>
<a id="__codelineno-10-26" name="__codelineno-10-26"></a><span class="w">        </span><span class="p">}</span>
<a id="__codelineno-10-27" name="__codelineno-10-27"></a><span class="w">      </span><span class="p">}</span>
<a id="__codelineno-10-28" name="__codelineno-10-28"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-10-29" name="__codelineno-10-29"></a><span class="w">    </span><span class="nx">vm</span><span class="p">.</span><span class="nx">component</span><span class="p">(</span><span class="s1">&#39;todo-item&#39;</span><span class="p">,</span><span class="w"> </span><span class="p">{</span>
<a id="__codelineno-10-30" name="__codelineno-10-30"></a><span class="w">      </span><span class="nx">props</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="s1">&#39;todo&#39;</span><span class="p">],</span>
<a id="__codelineno-10-31" name="__codelineno-10-31"></a><span class="w">      </span><span class="nx">template</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&lt;li&gt;{{ todo.text }}&lt;/li&gt;&#39;</span>
<a id="__codelineno-10-32" name="__codelineno-10-32"></a><span class="w">    </span><span class="p">});</span>
<a id="__codelineno-10-33" name="__codelineno-10-33"></a><span class="w">    </span><span class="nx">vm</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">)</span>
<a id="__codelineno-10-34" name="__codelineno-10-34"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>
<a id="__codelineno-10-35" name="__codelineno-10-35"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>
<a id="__codelineno-10-36" name="__codelineno-10-36"></a>
<a id="__codelineno-10-37" name="__codelineno-10-37"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
</li>
</ol>
<h3 id="_6">综合</h3>
<h4 id="_7">表单校验</h4>
<p>使用vue.js对<code>Html基础</code>实验中创建的<strong>学生表</strong>的表单数据进行校验，失败时在元素后边显示错误信息，至少实现以下规则：</p>
<table>
<thead>
<tr>
<th>元素</th>
<th>校验规则</th>
</tr>
</thead>
<tbody>
<tr>
<td>学号 (no)</td>
<td>- 不能为空<br>- 必须是数字</td>
</tr>
<tr>
<td>姓名 (name)</td>
<td>- 不能为空</td>
</tr>
<tr>
<td>性别 (gender)</td>
<td>- 必须是"男"或"女"</td>
</tr>
<tr>
<td>年龄 (age)</td>
<td>- 不能为空<br>- 必须是正整数</td>
</tr>
<tr>
<td>所在系 (dept)</td>
<td>- 不能为空</td>
</tr>
</tbody>
</table>
<p>代码模板如下：</p>
<div class="highlight"><table class="highlighttable"><tr><th colspan="2" class="filename"><span class="filename">04_Vue.js基础/src/vue_validation.html</span></th></tr><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-11-1"> 1</a></span>
<span class="normal"><a href="#__codelineno-11-2"> 2</a></span>
<span class="normal"><a href="#__codelineno-11-3"> 3</a></span>
<span class="normal"><a href="#__codelineno-11-4"> 4</a></span>
<span class="normal"><a href="#__codelineno-11-5"> 5</a></span>
<span class="normal"><a href="#__codelineno-11-6"> 6</a></span>
<span class="normal"><a href="#__codelineno-11-7"> 7</a></span>
<span class="normal"><a href="#__codelineno-11-8"> 8</a></span>
<span class="normal"><a href="#__codelineno-11-9"> 9</a></span>
<span class="normal"><a href="#__codelineno-11-10">10</a></span>
<span class="normal"><a href="#__codelineno-11-11">11</a></span>
<span class="normal"><a href="#__codelineno-11-12">12</a></span>
<span class="normal"><a href="#__codelineno-11-13">13</a></span>
<span class="normal"><a href="#__codelineno-11-14">14</a></span>
<span class="normal"><a href="#__codelineno-11-15">15</a></span>
<span class="normal"><a href="#__codelineno-11-16">16</a></span>
<span class="normal"><a href="#__codelineno-11-17">17</a></span>
<span class="normal"><a href="#__codelineno-11-18">18</a></span>
<span class="normal"><a href="#__codelineno-11-19">19</a></span>
<span class="normal"><a href="#__codelineno-11-20">20</a></span>
<span class="normal"><a href="#__codelineno-11-21">21</a></span>
<span class="normal"><a href="#__codelineno-11-22">22</a></span>
<span class="normal"><a href="#__codelineno-11-23">23</a></span>
<span class="normal"><a href="#__codelineno-11-24">24</a></span>
<span class="normal"><a href="#__codelineno-11-25">25</a></span>
<span class="normal"><a href="#__codelineno-11-26">26</a></span>
<span class="normal"><a href="#__codelineno-11-27">27</a></span>
<span class="normal"><a href="#__codelineno-11-28">28</a></span>
<span class="normal"><a href="#__codelineno-11-29">29</a></span>
<span class="normal"><a href="#__codelineno-11-30">30</a></span>
<span class="normal"><a href="#__codelineno-11-31">31</a></span>
<span class="normal"><a href="#__codelineno-11-32">32</a></span>
<span class="normal"><a href="#__codelineno-11-33">33</a></span>
<span class="normal"><a href="#__codelineno-11-34">34</a></span>
<span class="normal"><a href="#__codelineno-11-35">35</a></span>
<span class="normal"><a href="#__codelineno-11-36">36</a></span>
<span class="normal"><a href="#__codelineno-11-37">37</a></span>
<span class="normal"><a href="#__codelineno-11-38">38</a></span>
<span class="normal"><a href="#__codelineno-11-39">39</a></span>
<span class="normal"><a href="#__codelineno-11-40">40</a></span>
<span class="normal"><a href="#__codelineno-11-41">41</a></span>
<span class="normal"><a href="#__codelineno-11-42">42</a></span>
<span class="normal"><a href="#__codelineno-11-43">43</a></span>
<span class="normal"><a href="#__codelineno-11-44">44</a></span>
<span class="normal"><a href="#__codelineno-11-45">45</a></span>
<span class="normal"><a href="#__codelineno-11-46">46</a></span>
<span class="normal"><a href="#__codelineno-11-47">47</a></span>
<span class="normal"><a href="#__codelineno-11-48">48</a></span>
<span class="normal"><a href="#__codelineno-11-49">49</a></span>
<span class="normal"><a href="#__codelineno-11-50">50</a></span>
<span class="normal"><a href="#__codelineno-11-51">51</a></span>
<span class="normal"><a href="#__codelineno-11-52">52</a></span>
<span class="normal"><a href="#__codelineno-11-53">53</a></span>
<span class="normal"><a href="#__codelineno-11-54">54</a></span>
<span class="normal"><a href="#__codelineno-11-55">55</a></span>
<span class="normal"><a href="#__codelineno-11-56">56</a></span>
<span class="normal"><a href="#__codelineno-11-57">57</a></span>
<span class="normal"><a href="#__codelineno-11-58">58</a></span>
<span class="normal"><a href="#__codelineno-11-59">59</a></span>
<span class="normal"><a href="#__codelineno-11-60">60</a></span>
<span class="normal"><a href="#__codelineno-11-61">61</a></span>
<span class="normal"><a href="#__codelineno-11-62">62</a></span>
<span class="normal"><a href="#__codelineno-11-63">63</a></span>
<span class="normal"><a href="#__codelineno-11-64">64</a></span>
<span class="normal"><a href="#__codelineno-11-65">65</a></span>
<span class="normal"><a href="#__codelineno-11-66">66</a></span>
<span class="normal"><a href="#__codelineno-11-67">67</a></span>
<span class="normal"><a href="#__codelineno-11-68">68</a></span>
<span class="normal"><a href="#__codelineno-11-69">69</a></span>
<span class="normal"><a href="#__codelineno-11-70">70</a></span>
<span class="normal"><a href="#__codelineno-11-71">71</a></span>
<span class="normal"><a href="#__codelineno-11-72">72</a></span>
<span class="normal"><a href="#__codelineno-11-73">73</a></span>
<span class="normal"><a href="#__codelineno-11-74">74</a></span>
<span class="normal"><a href="#__codelineno-11-75">75</a></span>
<span class="normal"><a href="#__codelineno-11-76">76</a></span>
<span class="normal"><a href="#__codelineno-11-77">77</a></span>
<span class="normal"><a href="#__codelineno-11-78">78</a></span>
<span class="normal"><a href="#__codelineno-11-79">79</a></span>
<span class="normal"><a href="#__codelineno-11-80">80</a></span>
<span class="normal"><a href="#__codelineno-11-81">81</a></span>
<span class="normal"><a href="#__codelineno-11-82">82</a></span>
<span class="normal"><a href="#__codelineno-11-83">83</a></span>
<span class="normal"><a href="#__codelineno-11-84">84</a></span>
<span class="normal"><a href="#__codelineno-11-85">85</a></span></pre></div></td><td class="code"><div><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1"></a><span class="cp">&lt;!DOCTYPE html&gt;</span>  
<a id="__codelineno-11-2" name="__codelineno-11-2"></a><span class="p">&lt;</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">&quot;en&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-3" name="__codelineno-11-3"></a><span class="p">&lt;</span><span class="nt">head</span><span class="p">&gt;</span>  
<a id="__codelineno-11-4" name="__codelineno-11-4"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">charset</span><span class="o">=</span><span class="s">&quot;UTF-8&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-5" name="__codelineno-11-5"></a>  <span class="p">&lt;</span><span class="nt">meta</span> <span class="na">name</span><span class="o">=</span><span class="s">&quot;viewport&quot;</span> <span class="na">content</span><span class="o">=</span><span class="s">&quot;width=device-width, initial-scale=1.0&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-6" name="__codelineno-11-6"></a>  <span class="p">&lt;</span><span class="nt">title</span><span class="p">&gt;</span>学生表<span class="p">&lt;/</span><span class="nt">title</span><span class="p">&gt;</span>  
<a id="__codelineno-11-7" name="__codelineno-11-7"></a>  <span class="p">&lt;</span><span class="nt">script</span> <span class="na">src</span><span class="o">=</span><span class="s">&quot;vue.js&quot;</span><span class="p">&gt;&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>  
<a id="__codelineno-11-8" name="__codelineno-11-8"></a>  <span class="p">&lt;</span><span class="nt">style</span><span class="p">&gt;</span><span class="w">  </span>
<a id="__codelineno-11-9" name="__codelineno-11-9"></a><span class="w">    </span><span class="p">.</span><span class="nc">error-message</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-10" name="__codelineno-11-10"></a><span class="w">      </span><span class="k">color</span><span class="p">:</span><span class="w"> </span><span class="kc">red</span><span class="p">;</span><span class="w">  </span>
<a id="__codelineno-11-11" name="__codelineno-11-11"></a><span class="w">    </span><span class="p">}</span><span class="w">  </span>
<a id="__codelineno-11-12" name="__codelineno-11-12"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">style</span><span class="p">&gt;</span>  
<a id="__codelineno-11-13" name="__codelineno-11-13"></a><span class="p">&lt;/</span><span class="nt">head</span><span class="p">&gt;</span>  
<a id="__codelineno-11-14" name="__codelineno-11-14"></a><span class="p">&lt;</span><span class="nt">body</span><span class="p">&gt;</span>  
<a id="__codelineno-11-15" name="__codelineno-11-15"></a>  <span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;app&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-16" name="__codelineno-11-16"></a>    <span class="p">&lt;</span><span class="nt">h3</span><span class="p">&gt;</span>学生表<span class="p">&lt;/</span><span class="nt">h3</span><span class="p">&gt;</span>  
<a id="__codelineno-11-17" name="__codelineno-11-17"></a>    <span class="p">&lt;</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;container&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-18" name="__codelineno-11-18"></a>      <span class="p">&lt;</span><span class="nt">form</span> <span class="err">@</span><span class="na">submit</span><span class="err">.</span><span class="na">prevent</span><span class="o">=</span><span class="s">&quot;submitForm&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-19" name="__codelineno-11-19"></a>        <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>  
<a id="__codelineno-11-20" name="__codelineno-11-20"></a>          <span class="p">&lt;</span><span class="nt">label</span> <span class="na">for</span><span class="o">=</span><span class="s">&quot;no&quot;</span><span class="p">&gt;</span>学号:<span class="p">&lt;/</span><span class="nt">label</span><span class="p">&gt;</span>  
<a id="__codelineno-11-21" name="__codelineno-11-21"></a>          <span class="p">&lt;</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;text&quot;</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;no&quot;</span> <span class="na">v-model</span><span class="o">=</span><span class="s">&quot;form.no&quot;</span> <span class="na">placeholder</span><span class="o">=</span><span class="s">&quot;请输入学号..&quot;</span><span class="p">&gt;</span>  
<a id="__codelineno-11-22" name="__codelineno-11-22"></a>          <span class="p">&lt;</span><span class="nt">span</span> <span class="na">v-if</span><span class="o">=</span><span class="s">&quot;formErrors.no&quot;</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;error-message&quot;</span><span class="p">&gt;</span>{{ formErrors.no }}<span class="p">&lt;/</span><span class="nt">span</span><span class="p">&gt;</span>  
<a id="__codelineno-11-23" name="__codelineno-11-23"></a>        <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>  
<a id="__codelineno-11-24" name="__codelineno-11-24"></a>        <span class="cm">&lt;!-- 完整代码需要包含其他字段的校验 --&gt;</span>  
<a id="__codelineno-11-25" name="__codelineno-11-25"></a>        <span class="p">&lt;</span><span class="nt">button</span> <span class="na">type</span><span class="o">=</span><span class="s">&quot;submit&quot;</span><span class="p">&gt;</span>提交<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>  
<a id="__codelineno-11-26" name="__codelineno-11-26"></a>      <span class="p">&lt;/</span><span class="nt">form</span><span class="p">&gt;</span>  
<a id="__codelineno-11-27" name="__codelineno-11-27"></a>    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>  
<a id="__codelineno-11-28" name="__codelineno-11-28"></a>  <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>  
<a id="__codelineno-11-29" name="__codelineno-11-29"></a>
<a id="__codelineno-11-30" name="__codelineno-11-30"></a>  <span class="p">&lt;</span><span class="nt">script</span><span class="p">&gt;</span><span class="w">  </span>
<a id="__codelineno-11-31" name="__codelineno-11-31"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">ref</span><span class="p">,</span><span class="w"> </span><span class="nx">onMounted</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">;</span><span class="w">  </span>
<a id="__codelineno-11-32" name="__codelineno-11-32"></a>
<a id="__codelineno-11-33" name="__codelineno-11-33"></a><span class="w">    </span><span class="kd">const</span><span class="w"> </span><span class="nx">app</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">Vue</span><span class="p">.</span><span class="nx">createApp</span><span class="p">({</span><span class="w">  </span>
<a id="__codelineno-11-34" name="__codelineno-11-34"></a><span class="w">      </span><span class="nx">setup</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-35" name="__codelineno-11-35"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">form</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">ref</span><span class="p">({</span><span class="w">  </span>
<a id="__codelineno-11-36" name="__codelineno-11-36"></a><span class="w">          </span><span class="nx">no</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="w">  </span>
<a id="__codelineno-11-37" name="__codelineno-11-37"></a><span class="w">          </span><span class="c1">// 其他字段...  </span>
<a id="__codelineno-11-38" name="__codelineno-11-38"></a><span class="w">        </span><span class="p">});</span><span class="w">  </span>
<a id="__codelineno-11-39" name="__codelineno-11-39"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">formErrors</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">ref</span><span class="p">({</span><span class="w">  </span>
<a id="__codelineno-11-40" name="__codelineno-11-40"></a><span class="w">          </span><span class="nx">no</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="w">  </span>
<a id="__codelineno-11-41" name="__codelineno-11-41"></a><span class="w">          </span><span class="c1">// 其他字段的错误信息...  </span>
<a id="__codelineno-11-42" name="__codelineno-11-42"></a><span class="w">        </span><span class="p">});</span><span class="w">  </span>
<a id="__codelineno-11-43" name="__codelineno-11-43"></a>
<a id="__codelineno-11-44" name="__codelineno-11-44"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">validateForm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-45" name="__codelineno-11-45"></a><span class="w">          </span><span class="nx">formErrors</span><span class="p">.</span><span class="nx">value</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-46" name="__codelineno-11-46"></a><span class="w">            </span><span class="nx">no</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="w">  </span>
<a id="__codelineno-11-47" name="__codelineno-11-47"></a><span class="w">            </span><span class="c1">// 初始化其他字段的错误信息...  </span>
<a id="__codelineno-11-48" name="__codelineno-11-48"></a><span class="w">          </span><span class="p">};</span><span class="w">  </span>
<a id="__codelineno-11-49" name="__codelineno-11-49"></a>
<a id="__codelineno-11-50" name="__codelineno-11-50"></a><span class="w">          </span><span class="c1">// 校验学号  </span>
<a id="__codelineno-11-51" name="__codelineno-11-51"></a><span class="w">          </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">form</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">no</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-52" name="__codelineno-11-52"></a><span class="w">            </span><span class="nx">formErrors</span><span class="p">.</span><span class="nx">value</span><span class="p">.</span><span class="nx">no</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">&#39;学号不能为空&#39;</span><span class="p">;</span><span class="w">  </span>
<a id="__codelineno-11-53" name="__codelineno-11-53"></a><span class="w">            </span><span class="k">return</span><span class="w"> </span><span class="kc">false</span><span class="p">;</span><span class="w">  </span>
<a id="__codelineno-11-54" name="__codelineno-11-54"></a><span class="w">          </span><span class="p">}</span><span class="w">  </span>
<a id="__codelineno-11-55" name="__codelineno-11-55"></a>
<a id="__codelineno-11-56" name="__codelineno-11-56"></a><span class="w">          </span><span class="c1">// 校验其他字段...  </span>
<a id="__codelineno-11-57" name="__codelineno-11-57"></a>
<a id="__codelineno-11-58" name="__codelineno-11-58"></a><span class="w">          </span><span class="c1">// 如果没有错误，返回true  </span>
<a id="__codelineno-11-59" name="__codelineno-11-59"></a><span class="w">          </span><span class="k">return</span><span class="w"> </span><span class="nb">Object</span><span class="p">.</span><span class="nx">values</span><span class="p">(</span><span class="nx">formErrors</span><span class="p">.</span><span class="nx">value</span><span class="p">).</span><span class="nx">every</span><span class="p">(</span><span class="nx">error</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="nx">error</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="s1">&#39;&#39;</span><span class="p">);</span><span class="w">  </span>
<a id="__codelineno-11-60" name="__codelineno-11-60"></a><span class="w">        </span><span class="p">};</span><span class="w">  </span>
<a id="__codelineno-11-61" name="__codelineno-11-61"></a>
<a id="__codelineno-11-62" name="__codelineno-11-62"></a><span class="w">        </span><span class="kd">const</span><span class="w"> </span><span class="nx">submitForm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-63" name="__codelineno-11-63"></a><span class="w">          </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">validateForm</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-64" name="__codelineno-11-64"></a><span class="w">            </span><span class="c1">// 如果验证通过，可以执行表单提交逻辑  </span>
<a id="__codelineno-11-65" name="__codelineno-11-65"></a><span class="w">            </span><span class="c1">// 例如：使用axios发送POST请求到服务器  </span>
<a id="__codelineno-11-66" name="__codelineno-11-66"></a><span class="w">            </span><span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s1">&#39;Form submitted with data:&#39;</span><span class="p">,</span><span class="w"> </span><span class="nx">form</span><span class="p">.</span><span class="nx">value</span><span class="p">);</span><span class="w">  </span>
<a id="__codelineno-11-67" name="__codelineno-11-67"></a><span class="w">          </span><span class="p">}</span><span class="w">  </span>
<a id="__codelineno-11-68" name="__codelineno-11-68"></a><span class="w">        </span><span class="p">};</span><span class="w">  </span>
<a id="__codelineno-11-69" name="__codelineno-11-69"></a>
<a id="__codelineno-11-70" name="__codelineno-11-70"></a><span class="w">        </span><span class="nx">onMounted</span><span class="p">(()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-71" name="__codelineno-11-71"></a><span class="w">          </span><span class="c1">// 这里可以放置组件挂载后需要执行的代码  </span>
<a id="__codelineno-11-72" name="__codelineno-11-72"></a><span class="w">        </span><span class="p">});</span><span class="w">  </span>
<a id="__codelineno-11-73" name="__codelineno-11-73"></a>
<a id="__codelineno-11-74" name="__codelineno-11-74"></a><span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">{</span><span class="w">  </span>
<a id="__codelineno-11-75" name="__codelineno-11-75"></a><span class="w">          </span><span class="nx">form</span><span class="p">,</span><span class="w">  </span>
<a id="__codelineno-11-76" name="__codelineno-11-76"></a><span class="w">          </span><span class="nx">formErrors</span><span class="p">,</span><span class="w">  </span>
<a id="__codelineno-11-77" name="__codelineno-11-77"></a><span class="w">          </span><span class="nx">submitForm</span><span class="w">  </span>
<a id="__codelineno-11-78" name="__codelineno-11-78"></a><span class="w">        </span><span class="p">};</span><span class="w">  </span>
<a id="__codelineno-11-79" name="__codelineno-11-79"></a><span class="w">      </span><span class="p">}</span><span class="w">  </span>
<a id="__codelineno-11-80" name="__codelineno-11-80"></a><span class="w">    </span><span class="p">});</span><span class="w">  </span>
<a id="__codelineno-11-81" name="__codelineno-11-81"></a>
<a id="__codelineno-11-82" name="__codelineno-11-82"></a><span class="w">    </span><span class="nx">app</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">);</span><span class="w">  </span>
<a id="__codelineno-11-83" name="__codelineno-11-83"></a><span class="w">  </span><span class="p">&lt;/</span><span class="nt">script</span><span class="p">&gt;</span>  
<a id="__codelineno-11-84" name="__codelineno-11-84"></a><span class="p">&lt;/</span><span class="nt">body</span><span class="p">&gt;</span>  
<a id="__codelineno-11-85" name="__codelineno-11-85"></a><span class="p">&lt;/</span><span class="nt">html</span><span class="p">&gt;</span>
</code></pre></div></td></tr></table></div>
<h3 id="_8">拓展练习</h3>
<ol>
<li>使用vue.js实现课程表表单的校验</li>
</ol>












                
              </article>
            </div>
          
          
  <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

<script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script>
        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    
    <script id="__config" type="application/json">{"base": "..", "features": ["content.code.copy", "content.tabs.link", "navigation.expand", "navigation.sections", "navigation.path"], "search": "../assets/javascripts/workers/search.f8cc74c7.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script>
    
    
      <script src="../assets/javascripts/bundle.f1b6f286.min.js"></script>
      
    
  </body>
</html>